<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ProtocolSet;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;

// 协议配置
class ProtocolSetController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $keywords = $request->input('keywords', ''); // 查询关键字
        $is_all = $request->input('is_all', 0); //不分页获取全部数据

        if($is_all){
            $data = ProtocolSet::query()
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('name', 'like', "%{$keywords}%");
            })
            ->where($this->getAppWhere())
             ->orderBy('id')
            ->get()
            ->toArray();
        }else{
            $data = ProtocolSet::query()
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('name', 'like', "%{$keywords}%");
            })
            ->where($this->getAppWhere())
            ->orderByDesc('id')
            ->paginate(env('PAGE_SIZE', 10));
        }
       
        return showData($data);    
    }

    // 新增
    public function store(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'name'=> 'required',
            'street_id'=> 'required',
            'project_id'=> 'required',
            'village_id'=> 'required',
            'segment_id'=> 'required',
            'supply_form'=> 'required',
            'build_form'=> 'required',
        ], [
            'name.required'=> '请输入名称',
            'street_id.required'=> '请输入街道',
            'project_id.required'=> '请输入项目',
            'village_id.required'=> '请输入村庄',
            'segment_id.required'=> '请输入号段',
            'supply_form.required'=> '请输入补偿形式',
            'build_form.required'=> '请输入建筑形式',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $request->all();

        $find=ProtocolSet::query()
                        ->where('supply_form', $params['supply_form'])
                        ->where('build_form', $params['build_form'])
                        ->where('street_id', $params['street_id'])
                        ->where('village_id', $params['village_id'])
                        ->where('project_id', $params['project_id'])
                        ->where('segment_id', $params['segment_id'])
                        ->first();
        if($find){
            return showError('该类型配置已存在，请勿重复配置');
        }                


        isset($params['name'])?$Info['name']=$params['name']:'';
        isset($params['street_id'])?$Info['street_id']=$params['street_id']:'';
        isset($params['project_id'])?$Info['project_id']=$params['project_id']:'';
        isset($params['village_id'])?$Info['village_id']=$params['village_id']:'';
        isset($params['segment_id'])?$Info['segment_id']=$params['segment_id']:'';
        isset($params['supply_form'])?$Info['supply_form']=$params['supply_form']:'';
        isset($params['build_form'])?$Info['build_form']=$params['build_form']:'';
        isset($params['upper_lean'])?$Info['upper_lean']=$params['upper_lean']:'';
        isset($params['lower_lean'])?$Info['lower_lean']=$params['lower_lean']:'';
        isset($params['reward_area'])?$Info['reward_area']=$params['reward_area']:'';
        isset($params['subsidy_fee'])?$Info['subsidy_fee']=$params['subsidy_fee']:'';
        isset($params['transition_start_time'])?$Info['transition_start_time']=$params['transition_start_time']:'';
        isset($params['transition_end_time'])?$Info['transition_end_time']=$params['transition_end_time']:'';
        isset($params['transition_price'])?$Info['transition_price']=$params['transition_price']:'';
        isset($params['transition_month'])?$Info['transition_month']=$params['transition_month']:'';
        isset($params['require_sign_start_time'])?$Info['require_sign_start_time']=$params['require_sign_start_time']:'';
        isset($params['require_sign_end_time'])?$Info['require_sign_end_time']=$params['require_sign_end_time']:'';
        isset($params['require_sign_fee'])?$Info['require_sign_fee']=$params['require_sign_fee']:'';
        isset($params['require_clear_start_time'])?$Info['require_clear_start_time']=$params['require_clear_start_time']:'';
        isset($params['require_clear_end_time'])?$Info['require_clear_end_time']=$params['require_clear_end_time']:'';
        isset($params['require_clear_fee'])?$Info['require_clear_fee']=$params['require_clear_fee']:'';
        isset($params['decorate_price'])?$Info['decorate_price']=$params['decorate_price']:'';
        isset($params['no_decorate_price'])?$Info['no_decorate_price']=$params['no_decorate_price']:'';
        isset($params['stop_price'])?$Info['stop_price']=$params['stop_price']:'';
        isset($params['stop_month'])?$Info['stop_month']=$params['stop_month']:'';
        isset($params['improve_area'])?$Info['improve_area']=$params['improve_area']:'';
        isset($params['money_price'])?$Info['money_price']=$params['money_price']:'';
        isset($params['othe_build_price'])?$Info['othe_build_price']=$params['othe_build_price']:'';
        isset($params['public_area_scale'])?$Info['public_area_scale']=$params['public_area_scale']:'';
        isset($params['require_clear_time'])?$Info['require_clear_time']=$params['require_clear_time']:'';
        isset($params['reward_transition_price'])?$Info['reward_transition_price']=$params['reward_transition_price']:'';

        try {
            $add=ProtocolSet::query()->create($Info);
            return showSuccess('添加成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 编辑
    public function update($Id, Request $request)
    {
        if (empty($Id)) {
            return showError('id不能为空!');
        }
        $validate = Validator::make($request->all(), [
            'name'=> 'required',
            'street_id'=> 'required',
            'project_id'=> 'required',
            'village_id'=> 'required',
            'segment_id'=> 'required',
            'supply_form'=> 'required',
            'build_form'=> 'required',
        ], [
            'name.required'=> '请输入名称',
            'street_id.required'=> '请输入街道',
            'project_id.required'=> '请输入项目',
            'village_id.required'=> '请输入村庄',
            'segment_id.required'=> '请输入号段',
            'supply_form.required'=> '请输入补偿形式',
            'build_form.required'=> '请输入建筑形式',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $request->all();

        isset($params['name'])?$Info['name']=$params['name']:'';
        isset($params['street_id'])?$Info['street_id']=$params['street_id']:'';
        isset($params['project_id'])?$Info['project_id']=$params['project_id']:'';
        isset($params['village_id'])?$Info['village_id']=$params['village_id']:'';
        isset($params['segment_id'])?$Info['segment_id']=$params['segment_id']:'';
        isset($params['supply_form'])?$Info['supply_form']=$params['supply_form']:'';
        isset($params['build_form'])?$Info['build_form']=$params['build_form']:'';
        isset($params['upper_lean'])?$Info['upper_lean']=$params['upper_lean']:'';
        isset($params['lower_lean'])?$Info['lower_lean']=$params['lower_lean']:'';
        isset($params['reward_area'])?$Info['reward_area']=$params['reward_area']:'';
        isset($params['subsidy_fee'])?$Info['subsidy_fee']=$params['subsidy_fee']:'';
        isset($params['transition_start_time'])?$Info['transition_start_time']=$params['transition_start_time']:'';
        isset($params['transition_end_time'])?$Info['transition_end_time']=$params['transition_end_time']:'';
        isset($params['transition_price'])?$Info['transition_price']=$params['transition_price']:'';
        isset($params['transition_month'])?$Info['transition_month']=$params['transition_month']:'';
        isset($params['require_sign_start_time'])?$Info['require_sign_start_time']=$params['require_sign_start_time']:'';
        isset($params['require_sign_end_time'])?$Info['require_sign_end_time']=$params['require_sign_end_time']:'';
        isset($params['require_sign_fee'])?$Info['require_sign_fee']=$params['require_sign_fee']:'';
        isset($params['require_clear_start_time'])?$Info['require_clear_start_time']=$params['require_clear_start_time']:'';
        isset($params['require_clear_end_time'])?$Info['require_clear_end_time']=$params['require_clear_end_time']:'';
        isset($params['require_clear_fee'])?$Info['require_clear_fee']=$params['require_clear_fee']:'';
        isset($params['decorate_price'])?$Info['decorate_price']=$params['decorate_price']:'';
        isset($params['no_decorate_price'])?$Info['no_decorate_price']=$params['no_decorate_price']:'';
        isset($params['stop_price'])?$Info['stop_price']=$params['stop_price']:'';
        isset($params['stop_month'])?$Info['stop_month']=$params['stop_month']:'';
        isset($params['improve_area'])?$Info['improve_area']=$params['improve_area']:'';
        isset($params['money_price'])?$Info['money_price']=$params['money_price']:'';
        isset($params['othe_build_price'])?$Info['othe_build_price']=$params['othe_build_price']:'';
        isset($params['public_area_scale'])?$Info['public_area_scale']=$params['public_area_scale']:'';
        isset($params['require_clear_time'])?$Info['require_clear_time']=$params['require_clear_time']:'';
        isset($params['reward_transition_price'])?$Info['reward_transition_price']=$params['reward_transition_price']:'';

        try {
            $update = ProtocolSet::query()->where('id', $Id)->update($Info);
            return showSuccess('编辑成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 删除
    public function destroy($id)
    {
        if (empty($id)) {
            return showError('参数错误！');
        }

        $admin = ProtocolSet::query()->where('id', $id)->first();
        if (empty($admin)) {
            return showError('删除项不存在');
        }
        try {
            $delete = ProtocolSet::query()->where('id', $id)->delete();
            return showSuccess('删除成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 详情
    public function show($id)
    {
        if (empty($id)) {
            return showError('参数错误');
        }
   
        $data = ProtocolSet::query()
            ->where('id', $id)
            ->first();

        if (empty($data)) {
            return showError('获取信息失败');
        }

        return showData($data);
    }
}

